﻿Imports System.Web
Imports System.Web.Services
Imports System.Reflection
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Converters

Public Class hndNews
    Implements System.Web.IHttpHandler
    Public docsPath As String = ConfigurationManager.AppSettings("docsPath")
    Dim strhelp As New Helpers.Strings
    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Try
            Dim op As String = context.Request("op").Trim 'METODO DA RICHIAMARE
            Dim Params As String() = (From r As String In context.Request.Form.Keys Where r <> "op" Order By r _
                          Select context.Request.Form(r).Trim).ToArray
            If Params.Count = 0 Then
                Params = (From r As String In context.Request.QueryString.Keys Where r <> "op" And r <> "_" _
                          And r <> "callback" Order By r Ascending Select context.Request.QueryString(r).Trim).ToArray
            End If
            Dim t As Type = Me.GetType
            Dim m As MethodInfo = t.GetMethod(op)
            Dim result As String = String.Empty
            If m IsNot Nothing Then
                result = m.Invoke(Me, Params)
            End If
            context.Response.Write(result)
        Catch ex As Exception
            context.Response.Write("0")
        End Try
    End Sub
    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
    Public Function DeleteAllegato(ByVal IDAllegato As String, ByVal IDNews As String) As String
        Dim al As NewsAllegati = DAL.Context.NewsAllegati.Where(Function(n) n.ID.ToString = IDAllegato And n.IDNews.ToString = IDNews).Single
        If al IsNot Nothing Then
            DAL.Context.NewsAllegati.DeleteOnSubmit(al)
            DAL.Context.SubmitChanges()
        End If
        Return "1"
    End Function
    Public Function UpdateAllegato(ByVal Descrizione As String, ByVal IDAllegato As String, ByVal IDNews As String) As String
        Dim al As NewsAllegati = DAL.Context.NewsAllegati.Where(Function(n) n.ID.ToString = IDAllegato And n.IDNews.ToString = IDNews).Single
        If al IsNot Nothing Then
            al.Descrizione = Descrizione
            DAL.Context.SubmitChanges()
        End If
        Return "1"
    End Function
    Public Function GetLastNews(ByVal skipLimit As String, ByVal takeLimit As String) As String
        Dim news As IList = (From n In DAL.Context.News Where n.DataScadenza Is Nothing Or n.DataScadenza >= Date.Now _
                                 Order By n.DataPubblicazione Descending Select n.Titolo, Testo = strhelp.CleanHtml(If(n.Testo.Length > 100, n.Testo.Substring(0, 100), n.Testo)), n.ID, _
                                 DataPubblicazione = n.DataPubblicazione.ToShortDateString).Skip(skipLimit).Take(takeLimit).ToList
        Return JsonConvert.SerializeObject(news)
    End Function
End Class